<?php
/**
 * (ajax)
 */
namespace App\Admin\Ajax\Inventory;

use App\Models\Inventory\AreaCn;
use App\Models\Inventory\Transfer;
use App\Models\Inventory\TransferP;
use App\Models\Stocking\StockingSku;
use Encore\Admin\Facades\Admin;
use Illuminate\Support\Facades\DB;
class TransferPAjax {

    public function transferModal($id){
        return view('inventory.transfer-p.modal-transfer', compact('id'));
    }

    public function transferConfirm($param){
        try{
            $id = $param['id'];
            $iAreaId = $param['iarea_id'];
            $transferP = TransferP::query()->find($id);
            if($transferP->area_id == $iAreaId){
                return response()->json(['status' => false, 'message' => '请勿选择PUBLIC站点！']);
            }
            return response()->json(['status' => true, 'message' => '操作成功！']);
        }catch (\Exception $e){
            report($e);
            return response()->json(['status' => false, 'message' => '系统错误']);
        }
    }

    //减在途库存
    public function transferDeduct($param){
        try{
            $id = $param['id'];
            $iAreaId = $param['iarea_id'];
            $transferNum = $param['transfer_num'];
            DB::beginTransaction();
            $transferP = TransferP::query()->find($id);
            if($transferP->area_id == $iAreaId){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '请勿选择PUBLIC站点！']);
            }
            if($transferP->instant < $transferNum){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '公共库存数量不足！']);
            }
            $transferP->instant -= $transferNum;
            $flag1 = $transferP->save();
            if(!$flag1){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '公共库存扣减失败']);
            }
            $areaCn = AreaCn::query()->firstOrNew(['area_id' => $iAreaId,'psku_id' => $transferP->psku_id]);
            if($areaCn->area_transit < $transferNum){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '站点在途数量不足']);
            }
            $areaCn->area_transit -= $transferNum;
            $areaCn->instant += $transferNum;
            $flag3 = $areaCn->save();
            if(!$flag3){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '站点库存添加失败']);
            }
            $userId = Admin::user()->id;
            $transfer = Transfer::query()->create([
                'psku_id' => $transferP->psku_id,
                'iuser_id' => $userId,
                'iarea_id' => $iAreaId,
                'ouser_id' => $userId,
                'oarea_id' => $transferP->area_id,
                'deduct_transit' => 1,
                'transfer_num' => $transferNum,
                'status' => 2,
            ]);
            if($transfer){
                StockingSku::upCnInstant($areaCn.'_'.$transferP->psku_id);
                DB::commit();
                return response()->json(['status' => true, 'message' => '操作成功！']);
            }else{
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统错误']);
        }
    }

    public function transfer($param){
        try{
            $id = $param['id'];
            $iAreaId = $param['iarea_id'];
            $transferNum = $param['transfer_num'];
            DB::beginTransaction();

            //公共挪用
            $transferP = TransferP::query()->find($id);
            if($transferP->area_id == $iAreaId){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '请勿选择PUBLIC站点！']);
            }
            if($transferP->instant < $transferNum){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '公共库存数量不足！']);
            }
            $transferP->instant -= $transferNum;
            $flag1 = $transferP->save();
            if(!$flag1){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '公共库存扣减失败']);
            }

            //站点挪用
            $areaCn = AreaCn::query()->firstOrNew(['area_id' => $iAreaId,'psku_id' => $transferP->psku_id]);
            $areaCn->instant += $transferNum;
            $flag2 = $areaCn->save();
            if(!$flag2){
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '站点库存添加失败']);
            }

            //挪用记录
            $userId = Admin::user()->id;
            $transfer = Transfer::query()->create([
                'psku_id' => $transferP->psku_id,
                'iuser_id' => $userId,
                'iarea_id' => $iAreaId,
                'ouser_id' => $userId,
                'oarea_id' => $transferP->area_id,
                'deduct_transit' => 0,
                'transfer_num' => $transferNum,
                'status' => 2,
            ]);
            if($transfer){
                DB::commit();
                return response()->json(['status' => true, 'message' => '操作成功！']);
            }else{
                DB::rollBack();
                return response()->json(['status' => false, 'message' => '操作失败！']);
            }
        }catch (\Exception $e){
            DB::rollBack();
            report($e);
            return response()->json(['status' => false, 'message' => '系统错误']);
        }
    }
}
